package com.zenithmind.coding.controller;

import com.zenithmind.common.result.Result;
import com.zenithmind.coding.controller.base.BaseController;
import com.zenithmind.coding.mapper.ProblemStatisticsMapper.ProblemStatistics;
import com.zenithmind.coding.mapper.ProblemStatisticsMapper.TagStatistics;
import com.zenithmind.coding.mapper.ProblemStatisticsMapper.UserSolveStatistics;
import com.zenithmind.coding.service.ProblemStatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 问题统计控制器 - 遵循单一职责原则，专门处理问题统计相关功能
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Tag(name = "问题统计", description = "编程问题统计分析相关接口")
@RestController
@RequestMapping("/api/zenithMind/v1/problems/statistics")
@RequiredArgsConstructor
public class ProblemStatisticsController extends BaseController {

    private final ProblemStatisticsService problemStatisticsService;

    @Operation(summary = "获取问题统计", description = "获取编程问题统计信息")
    @GetMapping
    public Result<ProblemStatistics> getProblemStatistics(
            @Parameter(description = "分类ID") @RequestParam(required = false) String categoryId,
            @Parameter(description = "难度等级") @RequestParam(required = false) Integer difficulty) {
        ProblemStatistics statistics = problemStatisticsService.getProblemStatistics(categoryId, difficulty);
        return success(statistics);
    }

    @Operation(summary = "获取热门标签", description = "获取热门问题标签")
    @GetMapping("/tags/hot")
    public Result<List<TagStatistics>> getHotTags(
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "20") Integer limit) {
        List<TagStatistics> tags = problemStatisticsService.getHotTags(limit);
        return success(tags);
    }

    @Operation(summary = "获取用户解题统计", description = "获取用户解题统计信息")
    @GetMapping("/user")
    public Result<UserSolveStatistics> getUserSolveStatistics() {
        String userId = getCurrentUserId();
        UserSolveStatistics statistics = problemStatisticsService.getUserSolveStatistics(userId);
        return success(statistics);
    }

    @Operation(summary = "获取难度分布", description = "获取问题难度分布统计")
    @GetMapping("/difficulty-distribution")
    public Result<Object> getDifficultyDistribution(
            @Parameter(description = "分类ID") @RequestParam(required = false) String categoryId) {
        Object distribution = problemStatisticsService.getDifficultyDistribution(categoryId);
        return success(distribution);
    }

    @Operation(summary = "获取类型分布", description = "获取问题类型分布统计")
    @GetMapping("/type-distribution")
    public Result<Object> getTypeDistribution(
            @Parameter(description = "分类ID") @RequestParam(required = false) String categoryId) {
        Object distribution = problemStatisticsService.getTypeDistribution(categoryId);
        return success(distribution);
    }

    @Operation(summary = "获取提交趋势", description = "获取问题提交趋势数据")
    @GetMapping("/submission-trend")
    public Result<Object> getSubmissionTrend(
            @Parameter(description = "天数") @RequestParam(defaultValue = "30") Integer days,
            @Parameter(description = "问题ID") @RequestParam(required = false) String problemId) {
        Object trend = problemStatisticsService.getSubmissionTrend(days, problemId);
        return success(trend);
    }

    @Operation(summary = "获取通过率统计", description = "获取问题通过率统计")
    @GetMapping("/acceptance-rate")
    public Result<Object> getAcceptanceRateStatistics(
            @Parameter(description = "分类ID") @RequestParam(required = false) String categoryId,
            @Parameter(description = "难度等级") @RequestParam(required = false) Integer difficulty) {
        Object statistics = problemStatisticsService.getAcceptanceRateStatistics(categoryId, difficulty);
        return success(statistics);
    }

    @Operation(summary = "获取用户排行榜", description = "获取用户解题排行榜")
    @GetMapping("/leaderboard")
    public Result<Object> getLeaderboard(
            @Parameter(description = "排行榜类型") @RequestParam(defaultValue = "total") String type,
            @Parameter(description = "限制数量") @RequestParam(defaultValue = "50") Integer limit) {
        Object leaderboard = problemStatisticsService.getLeaderboard(type, limit);
        return success(leaderboard);
    }

    @Operation(summary = "获取活跃度统计", description = "获取用户活跃度统计")
    @GetMapping("/activity")
    public Result<Object> getActivityStatistics(
            @Parameter(description = "时间范围（天）") @RequestParam(defaultValue = "30") Integer days) {
        String userId = getCurrentUserId();
        Object activity = problemStatisticsService.getUserActivityStatistics(userId, days);
        return success(activity);
    }

    @Operation(summary = "获取学习进度", description = "获取用户学习进度统计")
    @GetMapping("/progress")
    public Result<Object> getLearningProgress(
            @Parameter(description = "分类ID") @RequestParam(required = false) String categoryId) {
        String userId = getCurrentUserId();
        Object progress = problemStatisticsService.getUserLearningProgress(userId, categoryId);
        return success(progress);
    }

    @Operation(summary = "获取能力雷达图", description = "获取用户能力雷达图数据")
    @GetMapping("/ability-radar")
    public Result<Object> getAbilityRadar() {
        String userId = getCurrentUserId();
        Object radar = problemStatisticsService.getUserAbilityRadar(userId);
        return success(radar);
    }

    @Operation(summary = "获取成长曲线", description = "获取用户成长曲线数据")
    @GetMapping("/growth-curve")
    public Result<Object> getGrowthCurve(
            @Parameter(description = "时间范围（天）") @RequestParam(defaultValue = "90") Integer days) {
        String userId = getCurrentUserId();
        Object curve = problemStatisticsService.getUserGrowthCurve(userId, days);
        return success(curve);
    }

    @Operation(summary = "获取全局统计", description = "获取平台全局统计信息")
    @GetMapping("/global")
    public Result<Object> getGlobalStatistics() {
        Object statistics = problemStatisticsService.getGlobalStatistics();
        return success(statistics);
    }
}
